//
//  KZRandomSphereView.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/9/7.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "KZRandomSphereView.h"

#import "KZVocabularyPackageModel.h"
#import "YoungSphere.h"


@implementation KZRandomSphereView
{
    YoungSphere *_sphereView;
    
    UIImageView *_emptyView;
}

- (instancetype)init {
    
    if (self = [super init]) {
        
        _emptyView = [UIImageView new];
        _emptyView.image = [UIImage imageNamed:@"kz-暂无数据"];
        [self addSubview:_emptyView];
        [_emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
        }];

    }
    return self;
}
- (void)setupUI {
    
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj != _emptyView) {
            [obj removeFromSuperview];
        }
    }];

    if (_vocabularys == nil) {
        _emptyView.hidden = true;
    } else {
        if (_vocabularys.count == 0) {
            _emptyView.hidden = false;
            return;
        } else {
            _emptyView.hidden = true;
        }
    }
    
    

    NSArray *colors = @[
                        [UIColor colorWithR:244 g:187 b:72 a:1],    // 黄
                        [UIColor colorWithR:206 g:92 b:226 a:1],    // 紫
                        [UIColor colorWithR:242 g:194 b:224 a:1],   // 粉
                        [UIColor colorWithR:105 g:216 b:98 a:1],    // 绿
                        [UIColor colorWithR:88 g:138 b:238 a:1],    // 蓝
                        ];
    // 调用展示
    _sphereView = [[YoungSphere alloc] initWithFrame:CGRectMake(20, 20, self.width-40, self.height-40)];
    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:0];
    for (NSInteger i = 0; i < _vocabularys.count; i ++) {
        KZVocabularyCurrentPackageDetailModel *model = _vocabularys[i];
       
        UIControl *gradientView = [UIControl new];
        [gradientView addTarget:self action:@selector(touchAction:) forControlEvents:UIControlEventTouchUpInside];
        [array addObject:gradientView];
        [_sphereView addSubview:gradientView];
        CGFloat width = W(80);
        gradientView.layer.cornerRadius = width / 2;
        gradientView.layer.masksToBounds = true;
        gradientView.frame = CGRectMake(0, 0, width, width);
        // 渐变图层
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.bounds = CGRectMake(0, 0, width, width);
        gradientLayer.position = CGPointMake(width/2, width/2);
        UIColor *color = colors[arc4random_uniform((uint32_t)colors.count)];
        gradientLayer.colors = @[
                                 (__bridge id)[[UIColor whiteColor] colorWithAlphaComponent:0.9].CGColor,
                                 (__bridge id)[color colorWithAlphaComponent:0.8].CGColor,
                                 ];
        gradientLayer.locations = @[@0.05];
        [gradientView.layer insertSublayer:gradientLayer atIndex:0];
        
        // 文字
        UILabel *label = [[UILabel alloc] init];
        label.frame = CGRectMake(3, 0, gradientView.width - 6, gradientView.height);
        label.adjustsFontSizeToFitWidth = true;
        label.numberOfLines = 1;
        label.text = model.Vocabulary;
        label.font = FONT(15/375.0*SCREEN_WIDTH);
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        [gradientView addSubview:label];
        NSShadow *shadow = [NSShadow new];
        shadow.shadowColor = [UIColor.blackColor colorWithAlphaComponent:0.8];
        shadow.shadowOffset = CGSizeMake(0, 1);
        shadow.shadowBlurRadius = 3;
        label.attributedText = [[NSAttributedString alloc] initWithString:label.text attributes:@{NSShadowAttributeName: shadow,/* NSVerticalGlyphFormAttributeName: @1*/}];
        
    }
    [_sphereView setCloudTags:array];
    [self addSubview:_sphereView];
    
    
}
- (void)setVocabularys:(NSArray *)vocabularys {
    
    _vocabularys = vocabularys;
    [self setupUI];
}

- (void)touchAction:(UIControl *)sender {
    
    CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:@"transform"];
    ani.duration = 0.5;
    ani.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    ani.repeatCount = 1;
    ani.autoreverses = true;
    CATransform3D t1 = CATransform3DIdentity;
    t1 = CATransform3DScale(t1, 1.5, 1.5, 1);
    ani.toValue = [NSValue valueWithCATransform3D:t1];
    [sender.layer addAnimation:ani forKey:@"ani"];
}
- (void)removeAnimation {
    
    [_sphereView timerFree];
}
@end
